package com.sangeng.controller;

import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.UpdateArticleDto;
import com.sangeng.domain.dto.AddArticleDto;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.domain.vo.UpdateArticleVo;
import com.sangeng.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/content/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 添加博文
     * @param addArticledto
     * @return
     */
    @PostMapping
    public ResponseResult addArticle(@RequestBody AddArticleDto addArticledto)
    {
        return articleService.addArticle(addArticledto);
    }

    /**
     *在后台需要分页查询文章功能，要求能根据标题和摘要模糊查询。
     * @param pageNum
     * @param pageSize
     * @param title
     * @param summary
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> selectAllByPage(int pageNum,int pageSize,String title,String summary){
       return articleService.selectAllByPage(pageNum,pageSize,title,summary);
    }

    /**
     * 根据文章id查询数据，相应的文章所属标签也要查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult<UpdateArticleVo> selectById(@PathVariable Long id){
        return articleService.selectById(id);
    }

    /**
     * 修改文章数据，同时要要进行文章标签表的更新
     * @param updateArticleDto
     * @return
     */
    @PutMapping
    public ResponseResult updateData(@RequestBody UpdateArticleDto updateArticleDto)
    {
        return articleService.updateData(updateArticleDto);
    }

    /**
     * 根绝文章id删除文章，同时也要删除文章标签表中的数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable Long id){
        return articleService.deleteById(id);
    }
}
